package nl.project.source;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

/**
 *
 * @author Wouter
 */

@SuppressWarnings("serial")
public class TestDataFiller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
//            TestDataGetter tdg = new TestDataGetter();
        	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        	Key enrollmentsKey = KeyFactory.createKey("testData", "testEnrollment");
        	
        	Query query = new Query("enrollment", enrollmentsKey);
        	List<Entity> enrollments = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
        	
        	
        	ArrayList contact = (ArrayList) enrollments.get(0).getProperty("contact");
        	
//        	out.println(enrollments.get(0).toString());
        	out.println(enrollments.get(0).getProperty("parentId"));
        	out.println(contact.get(0));
        	out.println(enrollments.size());
            out.println("It worked!");
        } catch(Exception e) {
        	out.println("It didn't work!");
        	out.println(e);
        } finally {
            
        }
    }

   /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
